package com.bmps.springcloud.insuredmicroservice.config.redisson;

import io.micrometer.core.instrument.util.StringUtils;
import lombok.Data;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.redisson.config.ReadMode;
import org.redisson.config.SentinelServersConfig;
import org.redisson.config.SingleServerConfig;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * description:
 * 哨兵模式RedissonClient配置
 *
 * @author jackdaw
 * @date 2019-11-28 16:26
 */


@Configuration
//@ConfigurationProperties(prefix = "spring.redis.sentinel")
@ConfigurationProperties(prefix = "spring.redis")
@Data
public class RedissonClientConfig {

    private String host;
    private String port;
    private String password;
    private int database;
//    private final RedisProperties redisProperties;

//    public RedissonClientConfig(RedisProperties redisProperties) {
//        this.redisProperties = redisProperties;
//    }

    @Bean
    RedissonClient redissonSentinel() {
        Config config = new Config();
        //String[] nodes = redisProperties.getSentinel().getNodes().split(",");
//        SentinelServersConfig serverConfig = config.useSentinelServers()
//                .addSentinelAddress(nodes)
//                .setMasterName(redisProperties.getSentinel().getMaster())
//                .setReadMode(ReadMode.SLAVE)
//                .setTimeout(redisProperties.getTimeout());
        SingleServerConfig serverConfig = config.useSingleServer();
        serverConfig.setAddress(host + ":" + port);
        if (StringUtils.isNotBlank(password)) {
            serverConfig.setPassword(password);
        }
        if (database != 0) {
            serverConfig.setDatabase(database);
        }
        return Redisson.create(config);

    }
}
